<template>
  <div>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="DepartAdd-form"
    >
      <el-form-item label="部门名字" prop="name">
        <el-input
          type="text"
          v-model="ruleForm.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门电话" prop="tel">
        <el-input
          type="tel"
          v-model="ruleForm.tel"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addDepart, getDepart } from "../api/depart.js";
import { validDepartTel, validDepartName } from "../util/validate.js";
import { mapState, mapMutations } from "vuex";
export default {
  name: "Admin-depart-add",
  computed: {
    ...mapState(["depart"]),
  },
  methods: {
    ...mapMutations(["setDepart"]),
    async addInfos() {
      await addDepart(this.ruleForm);
    },
    submitForm() {
      this.$refs["ruleForm"].validate(async (valid) => {
        if (valid) {
          this.addInfos();

          let infos = await getDepart();
          let departs = [];
          for (let val in infos.data) {
            departs[val] = infos.data[val].name;
          }
          this.setDepart(departs);

          this.$message({
            message: "信息添加成功！",
            type: "success",
          });
        } else {
          this.$message.error("信息填写不完整或格式不正确！");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs["ruleForm"].resetFields();
    },
  },
  data() {
    /* 验证部门名字是否合法（不能重复添加相同的部门） */
    const validateDepartName = async (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入部门名字"));
      } else {
        if (!(await validDepartName(value))) {
          callback(new Error("该部门已存在，不用重复添加吖"));
        }
        callback();
      }
    };
    // 验证电话号码
    const validateDepartTel = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入联系电话"));
      } else {
        if (!validDepartTel(value)) {
          callback(new Error("电话号码格式不正确"));
        }
        callback();
      }
    };
    return {
      ruleForm: {
        name: "",
        tel: "",
      },
      rules: {
        name: [
          {
            required: true,
            trigger: "blur",
            validator: validateDepartName,
          },
          {
            min: 2,
            max: 10,
            message: "姓名长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
        tel: [
          { required: true, trigger: "blur", validator: validateDepartTel },
        ],
      },
    };
  },
};
</script>

<style scoped lang="less">
.DepartAdd-form {
  width: 400px;
  height: 200px;
}
.el-radio--medium.is-bordered {
  margin: 10px;
}
</style>